unit usitools;

interface
uses Classes, SysUtils, uni;

type
  TMYTable = class(TPersistent)
  private
    FMyColumns: TStrings;
    FSymbols: TStrings;
    FTableName: string;
    function GetTables: TStrings;
    procedure loadColumn;
  public
    constructor Create;
    destructor Destroy; override;
    function SQL_SYMBOL: string;
    property MyColumns: TStrings read FMyColumns write FMyColumns;
    property TableName: string read FTableName write FTableName;
    property Tables: TStrings read GetTables;
  end;

  TMyColumn = class(TPersistent)
  end;

implementation
uses uMainDataModule;



{
*********************************** TMYTable ***********************************
}

constructor TMYTable.Create;
begin
  inherited Create;
  FMyColumns := TStringList.Create;
  FSymbols := TStringList.Create;
end;

destructor TMYTable.Destroy;
begin
  FreeAndNil(FSymbols);
  FreeAndNil(FMyColumns);
  inherited Destroy;
end;

function TMYTable.GetTables: TStrings;
var
  Temp_Query: TUniQuery;
begin
  Result := TStringList.Create;
  Temp_Query := TUniQuery.Create(nil);
  try
    with Temp_Query do begin
      Connection := dmMain.uConn;
      Sql.Text := Format('select table_name from user_tables', []);
      Open;
      First;
      while not eof do begin
        Result.Add(FieldByName('table_name').AsString);
        next;
      end;
    end;
  finally
    FreeAndNil(Temp_Query);
  end;
end;


procedure TMYTable.loadColumn;
var
  Temp_Query: TUniQuery;
begin
  Temp_Query := TUniQuery.Create(nil);
  try
    with Temp_Query do begin
      Connection := dmMain.uConn;
      Sql.Text := Format('select t.COLUMN_NAME,t.DATA_TYPE' + #13#10 +
        ' from user_tab_columns t' + #13#10 +
        ' where  t.table_name =upper(%s)', [QuotedStr(TableName)]);
      Open;
      First;
      while not eof do begin
        FMyColumns.Add(FieldByName('COLUMN_NAME').AsString);
        if FieldByName('DATA_TYPE').AsString = 'NUMBER' then
          FSymbols.Add('%d');
        if FieldByName('DATA_TYPE').AsString = 'VARCHAR2' then
          FSymbols.Add('%s');
        if FieldByName('DATA_TYPE').AsString = 'DATE' then
          FSymbols.Add('%s');
        if FieldByName('DATA_TYPE').AsString = 'FLOAT' then
          FSymbols.Add('%f');
        next;
      end;
    end;
  finally
    FreeAndNil(Temp_Query);
  end;
end;

function TMYTable.SQL_SYMBOL: string;
begin
  loadColumn;
  Result := UpperCase(Format('Insert into %0:s(%1:s) Values(%2:s)', [TableName, FMyColumns.CommaText, FSymbols.CommaText]));
end;


end.

